#!/usr/bin/env python

""" 
based on http://backbone.drop.io
data attributes of Resource instances mirror what the drop.io api returns
"""

__author__ = "jimmyorr@gmail.com (Jimmy Orr), chedbrandh@gmail.com (Christofer Hedbrandh)"
__version__ = "0.2.0"

import calendar
import time

class Resource():
    def __init__(self, resource_dict):
        assert resource_dict
        for (k, v) in resource_dict.items():
            setattr(self, k, v)
    
    def __eq__(self, other):
        return vars(self) == vars(other)


class Drop(Resource):
    def __init__(self, drop_dict):
        Resource.__init__(self, drop_dict)
    
    def __str__(self):
        return self.name

    __repr__ = __str__
    
class Asset(Resource):
    def __init__(self, asset_dict):
        Resource.__init__(self, asset_dict)
        # convert roles list to dict
        if hasattr(self, 'roles'):
            self.roles = dict([(x['name'], x) for x in self.roles])
            # convert locations list to dict
            for role in self.roles.values():
                if 'locations' in role:
                    role['locations'] = dict([(x['name'], x) for x in role['locations']])
    
    def __str__(self):
        return self.name

    __repr__ = __str__

class Subscription(Resource):
    def __init__(self, subscription_dict):
        Resource.__init__(self, subscription_dict)

# todo currently not used due to changing date time format
def _iso8601_to_epoch(time_str):
    t = time.strptime(time_str[:19], '%Y-%m-%dT%H:%M:%S')
    prefix = time_str[19]
    sec_offset = 0
    if not prefix == 'Z':
        sec_offset += int(time_str[-5:-3]) * 60 * 60
        sec_offset += int(time_str[-2:]) * 60
        sec_offset *= 1 if prefix == '-' else -1
    return calendar.timegm(t) + sec_offset
